package com.dnndo.common.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 自定义异常类
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class CustomExceptions extends RuntimeException{

    private String msg;
    private int code;
    private String isRegion;

    public CustomExceptions(String msg, int code)
    {
        super(msg);
        this.msg = msg;
        this.code = code;
    }
    public CustomExceptions(String msg)
    {
        super(msg);
        this.msg = msg;
        this.code = 500;
    }
    public CustomExceptions(String msg, int code, String isRegion)
    {
        super(msg);
        this.msg = msg;
        this.code = code;
        this.isRegion = isRegion;
    }
    public CustomExceptions(String msg, String isRegion)
    {
        super(msg);
        this.msg = msg;
        this.code = 500;
        this.isRegion = isRegion;
    }
    @Override
    public String toString() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "{\"msg\":\"" + msg + "\",\"code\":" + code + "}";
        }
    }
}
